package com.help.knowhelps.ui;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;

import com.help.knowhelps.R;
import com.help.knowhelps.R.string;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class FindBackActivity extends Activity{

	EditText et_name,et_pwd,et_repwd;
	View submit;
	String phone;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.registerinfo_activity);
		phone = getIntent().getStringExtra("phoneNum");
		initComponent();
	}
	private void initComponent()
	{
		UiUtils.InitActionBar(this, "修改密码", UiUtils.ACTIONBAR_BUTTON_DEFAULT, 
				UiUtils.ACTIONBAR_BUTTON_DISAPPEAR ,clicklistener);
		et_name = (EditText) findViewById(R.id.registerinfo_editText_name);
		et_name.setVisibility(View.GONE);
		et_pwd = (EditText) findViewById(R.id.registerinfo_editText_pwd);
		et_repwd = (EditText) findViewById(R.id.registerinfo_editText_repwd);
		submit = findViewById(R.id.registerinfo_TextView_submit);
		submit.setOnClickListener(clicklistener);
		et_pwd.setOnFocusChangeListener(new OnFocusChangeListener()
		{

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus){
					if(et_pwd.getText().toString().trim().length()<8){
						Toast.makeText(FindBackActivity.this, "密码不能小于8个字符", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		et_repwd.setOnFocusChangeListener(new OnFocusChangeListener()
		{
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus){
					if(!et_repwd.getText().toString().trim().equals(et_pwd.getText().toString().trim())){
						Toast.makeText(FindBackActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show(); 
					}
				}
			}
		});
		
	}
//			registerinfo_submit.setOnClickListener(new OnClickListener(){
//
//				@Override
//				public void onClick(View v) {
//					
//					if(!checkEdit()){
//						return;
//					}
//					// TODO Auto-generated method stub
//					String httpUrl="http://192.168.1.100:8080/web-test/register.jsp";
//					HttpPost httpRequest=new HttpPost(httpUrl);
//					List<NameValuePair> params=new ArrayList<NameValuePair>();
//					params.add(new BasicNameValuePair("username",registerinfo_username.getText().toString().trim()));
//					params.add(new BasicNameValuePair("password",registerinfo_passwd.getText().toString().trim()));
//					HttpEntity httpentity = null;
//					try {
//						httpentity = new UrlEncodedFormEntity(params,"utf8");
//					} catch (UnsupportedEncodingException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					httpRequest.setEntity(httpentity);
//					HttpClient httpclient=new DefaultHttpClient();
//					HttpResponse httpResponse = null;
//					try {
//						httpResponse = httpclient.execute(httpRequest);
//					} catch (ClientProtocolException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					if(httpResponse.getStatusLine().getStatusCode()==200)
//					{
//						String strResult = null;
//						try {
//							strResult = EntityUtils.toString(httpResponse.getEntity());
//						} catch (ParseException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						} catch (IOException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//						Toast.makeText(UserRegister.this, strResult, Toast.LENGTH_SHORT).show();
//					}
//					else
//					{
//						Toast.makeText(UserRegister.this, "请求错误", Toast.LENGTH_SHORT).show();
//					}
//					
//				}
//				
//			});
//		}
	
	private boolean checkEdit(){
		 if (et_pwd.getText().toString().trim().equals("")) {
			Toast.makeText(FindBackActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
		} else if (!et_pwd.getText().toString().trim().equals(et_repwd.getText().toString().trim())) {
			Toast.makeText(FindBackActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
		} else {
			return true;
		}
		return false;
	}
	private OnClickListener clicklistener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.actionbar_imageView_left:
				finish();
				break;
			case R.id.registerinfo_TextView_submit:
				if(!checkEdit())
					return;
				String code = et_pwd.getText().toString().trim();
				// phone + code请求修改密码,并回调登陆成功.
				finish();
				break;
			default:
				break;
			}
		}
	};
}